<?php

class AdminController extends Controller
{
	public $layout='//layouts/admin/main';

	public function init(){
        Yii::app()->user->loginUrl = array('/admin/site/login');
        parent::init();
    }

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('login','logout'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update', 'index', 'view', 'new', 'edit'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

  public function actionEdit()
  {
    $admin = Admin::model()->find('username = :username', array(':username' => Yii::app()->user->name));
    $this->render('edit', array(
      'admin' => $admin,
    ));
  }

  public function actionUpdate()
  {
    $admin = Admin::model()->find('username = :username', array(':username' => Yii::app()->user->name));

    if($_POST['Admin'])
    {
      $admin->password = $_POST['change_password'];

      if($admin->save())
      {
        Yii::app()->user->setFlash('success', 'Success');
        Yii::app()->user->setFlash('content', 'You have successfully updated your password.');

				$this->redirect(array('edit'));
      }
    }

    $this->render('edit', array(
      'admin' => $admin,
    ));
  }
}
